package com.zh.freechat.common.resp;

import lombok.Getter;
import lombok.Setter;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.List;

import static com.zh.freechat.common.CustomConst.PINYIN_FORMAT;


/**
 * 用户联系人列表 VO对象
 *
 * @author ZH
 * @date 13:41 2021/8/18
 */
@Getter
@Setter
public class UserContactsVO implements Serializable {
    /**
     * 用户ID
     */
    private String userId;
    /**
     * 我的好友
     */
    private List<ContactsItem> friends;
    /**
     * 用户黑名单
     */
    private List<String> blackIds;


    @Getter
    @Setter
    public static class ContactsItem implements Serializable {
        private String relId;
        /**
         * 联系人ID
         */
        private String userId;
        /**
         * 备注名
         */
        private String aliasName;
        /**
         * 账号
         */
        private String username;
        /**
         * 头像
         */
        private String avatarUrl;
        /**
         * 昵称
         */
        private String nickname;
        /**
         * 性别
         */
        private Integer gender;

        /**
         * 首字母
         */
        private String firstLetter;

        public ContactsItem() {
        }

        public ContactsItem(String relId, String userId, String username) {
            this.relId = relId;
            this.userId = userId;
            this.username = username;
        }

        /**
         * 写入首字母
         */
        public void writeFirstLetter() {
            //aliasName、nickname
            try {
                String name = StringUtils.hasText(aliasName) ? aliasName : nickname;
                char first = name.charAt(0);
                if ('\u4e00' < first && first < '\u9fa5') {
                    String[] str = PinyinHelper.toHanyuPinyinStringArray(first, PINYIN_FORMAT);
                    char letter = str[0].charAt(0);
                    this.firstLetter = String.valueOf(letter);
                } else {
                    this.firstLetter = String.valueOf(first);
                }
            } catch (Exception ex) {
                this.firstLetter = "#";
            }
        }
    }

}
